package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.NAsphaltMatchType;
import com.ruoyi.zhgd.mapper.NAsphaltMatchTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltMatchThresholdBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltMatchThresholdVo;
import com.ruoyi.zhgd.domain.NAsphaltMatchThreshold;
import com.ruoyi.zhgd.mapper.NAsphaltMatchThresholdMapper;
import com.ruoyi.zhgd.service.INAsphaltMatchThresholdService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青筛分级配Service业务层处理
 *
 * @author tll
 * @date 2023-10-12
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltMatchThresholdServiceImpl implements INAsphaltMatchThresholdService {

    private final NAsphaltMatchThresholdMapper baseMapper;

    private final NAsphaltMatchTypeMapper nAsphaltMatchTypeMapper;

    /**
     * 查询沥青筛分级配
     */
    @Override
    public NAsphaltMatchThresholdVo queryById(String id){
        return baseMapper.getById(id);
    }

    /**
     * 查询沥青筛分级配列表
     */
    @Override
    public TableDataInfo<NAsphaltMatchThresholdVo> queryPageList(NAsphaltMatchThresholdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatchThreshold> lqw = buildQueryWrapper(bo);
        Page<NAsphaltMatchThresholdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青筛分级配列表
     */
    @Override
    public List<NAsphaltMatchThresholdVo> queryList(NAsphaltMatchThresholdBo bo) {
        LambdaQueryWrapper<NAsphaltMatchThreshold> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltMatchThreshold> buildQueryWrapper(NAsphaltMatchThresholdBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltMatchThreshold> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NAsphaltMatchThreshold::getDeptPkid, bo.getDeptPkid());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), NAsphaltMatchThreshold::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchType()), NAsphaltMatchThreshold::getMatchType, bo.getMatchType());
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), NAsphaltMatchThreshold::getMatchName, bo.getMatchName());
        lqw.eq(bo.getSieve0d075min() != null, NAsphaltMatchThreshold::getSieve0d075min, bo.getSieve0d075min());
        lqw.eq(bo.getSieve0d075max() != null, NAsphaltMatchThreshold::getSieve0d075max, bo.getSieve0d075max());
        lqw.eq(bo.getSieve0d15min() != null, NAsphaltMatchThreshold::getSieve0d15min, bo.getSieve0d15min());
        lqw.eq(bo.getSieve0d15max() != null, NAsphaltMatchThreshold::getSieve0d15max, bo.getSieve0d15max());
        lqw.eq(bo.getSieve0d3min() != null, NAsphaltMatchThreshold::getSieve0d3min, bo.getSieve0d3min());
        lqw.eq(bo.getSieve0d3max() != null, NAsphaltMatchThreshold::getSieve0d3max, bo.getSieve0d3max());
        lqw.eq(bo.getSieve0d6min() != null, NAsphaltMatchThreshold::getSieve0d6min, bo.getSieve0d6min());
        lqw.eq(bo.getSieve0d6max() != null, NAsphaltMatchThreshold::getSieve0d6max, bo.getSieve0d6max());
        lqw.eq(bo.getSieve1d18min() != null, NAsphaltMatchThreshold::getSieve1d18min, bo.getSieve1d18min());
        lqw.eq(bo.getSieve1d18max() != null, NAsphaltMatchThreshold::getSieve1d18max, bo.getSieve1d18max());
        lqw.eq(bo.getSieve2d36min() != null, NAsphaltMatchThreshold::getSieve2d36min, bo.getSieve2d36min());
        lqw.eq(bo.getSieve2d36max() != null, NAsphaltMatchThreshold::getSieve2d36max, bo.getSieve2d36max());
        lqw.eq(bo.getSieve4d75min() != null, NAsphaltMatchThreshold::getSieve4d75min, bo.getSieve4d75min());
        lqw.eq(bo.getSieve4d75max() != null, NAsphaltMatchThreshold::getSieve4d75max, bo.getSieve4d75max());
        lqw.eq(bo.getSieve9d5min() != null, NAsphaltMatchThreshold::getSieve9d5min, bo.getSieve9d5min());
        lqw.eq(bo.getSieve9d5max() != null, NAsphaltMatchThreshold::getSieve9d5max, bo.getSieve9d5max());
        lqw.eq(bo.getSieve13d2min() != null, NAsphaltMatchThreshold::getSieve13d2min, bo.getSieve13d2min());
        lqw.eq(bo.getSieve13d2max() != null, NAsphaltMatchThreshold::getSieve13d2max, bo.getSieve13d2max());
        lqw.eq(bo.getSieve16min() != null, NAsphaltMatchThreshold::getSieve16min, bo.getSieve16min());
        lqw.eq(bo.getSieve16max() != null, NAsphaltMatchThreshold::getSieve16max, bo.getSieve16max());
        lqw.eq(bo.getSieve19min() != null, NAsphaltMatchThreshold::getSieve19min, bo.getSieve19min());
        lqw.eq(bo.getSieve19max() != null, NAsphaltMatchThreshold::getSieve19max, bo.getSieve19max());
        lqw.eq(bo.getSieve26d5min() != null, NAsphaltMatchThreshold::getSieve26d5min, bo.getSieve26d5min());
        lqw.eq(bo.getSieve26d5max() != null, NAsphaltMatchThreshold::getSieve26d5max, bo.getSieve26d5max());
        lqw.eq(bo.getSieve31d5min() != null, NAsphaltMatchThreshold::getSieve31d5min, bo.getSieve31d5min());
        lqw.eq(bo.getSieve31d5max() != null, NAsphaltMatchThreshold::getSieve31d5max, bo.getSieve31d5max());
        lqw.eq(bo.getSieve37d5min() != null, NAsphaltMatchThreshold::getSieve37d5min, bo.getSieve37d5min());
        lqw.eq(bo.getSieve37d5max() != null, NAsphaltMatchThreshold::getSieve37d5max, bo.getSieve37d5max());
        lqw.eq(bo.getSieve52min() != null, NAsphaltMatchThreshold::getSieve52min, bo.getSieve52min());
        lqw.eq(bo.getSieve52max() != null, NAsphaltMatchThreshold::getSieve52max, bo.getSieve52max());
        lqw.eq(bo.getSieve53min() != null, NAsphaltMatchThreshold::getSieve53min, bo.getSieve53min());
        lqw.eq(bo.getSieve53max() != null, NAsphaltMatchThreshold::getSieve53max, bo.getSieve53max());
        lqw.eq(bo.getCreator() != null, NAsphaltMatchThreshold::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NAsphaltMatchThreshold::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NAsphaltMatchThreshold::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NAsphaltMatchThreshold::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增沥青筛分级配
     */
    @Override
    public Boolean insertByBo(NAsphaltMatchThresholdBo bo) {
        NAsphaltMatchThreshold add = BeanUtil.toBean(bo, NAsphaltMatchThreshold.class);
        validEntityBeforeSave(add);
        add.setCreator(LoginHelper.getUserId());
        add.setCreateDate(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青筛分级配
     */
    @Override
    public Boolean updateByBo(NAsphaltMatchThresholdBo bo) {
        NAsphaltMatchThreshold update = BeanUtil.toBean(bo, NAsphaltMatchThreshold.class);
        validEntityBeforeSave(update);
        update.setUpdater(LoginHelper.getUserId());
        update.setUpdateDate(new Date());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltMatchThreshold entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青筛分级配
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据级配名称获取上下限信息
     *
     * @param matchName 配方名称
     * @return 上下限信息
     */
    @Override
    public NAsphaltMatchThreshold getByMatchName(String matchName) {
        if(ObjectUtils.isEmpty(matchName)) {
            return null;
        }
        QueryWrapper<NAsphaltMatchThreshold> queryWrapper = new QueryWrapper<NAsphaltMatchThreshold>();
        queryWrapper.eq("match_name", matchName);
        List<NAsphaltMatchThreshold> list = baseMapper.selectList(queryWrapper);
        return null == list || 0 == list.size() ? null : list.get(0);
    }

    /**
     * 获取上下限分页数据
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltMatchThresholdVo> page(NAsphaltMatchThresholdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatchThreshold> lqw = buildQueryWrapper(bo);
        return TableDataInfo.build(baseMapper.page(pageQuery.build(), lqw));
    }

    /**
     * 获取上下限信息
     * @param id
     * @return
     */
    @Override
    public NAsphaltMatchThresholdVo find(String id) {
        NAsphaltMatchThresholdVo vo = baseMapper.getById(id);
        QueryWrapper<NAsphaltMatchType> wrapper = new QueryWrapper<>();
        wrapper.like("type", vo.getMatchType());
        List<NAsphaltMatchType> typeList = nAsphaltMatchTypeMapper.selectList(wrapper);
        NAsphaltMatchType typeEntity = null;
        if (ObjectUtils.isNotEmpty(typeList)) {
            typeEntity = typeList.get(0);
        }
        if (ObjectUtils.isNotEmpty(typeEntity)) {
            vo.setSieveArray(typeEntity.getSieveArray());
        } else {
            vo.setSieveArray("");
        }
        return vo;
    }
}
